package com.bookmarket.controller;

import com.bookmarket.entity.*;
import com.bookmarket.service.LQMainService;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/main")
public class LQ_MainController {

    @Resource
    private LQMainService mainService;

//    查询类型
    @RequestMapping("/querySType")
    public ModelAndView querySType(){
        List<Stype> list = mainService.querySType();
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("stype",list);
        return mv;
    }

//    显示所有类型
    @RequestMapping("/queryAllSType")
    public ModelAndView queryAllSType(){
        List<Stype> list = mainService.queryAllSType();
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("stype",list);
        return mv;
    }

//    查询图书
    @RequestMapping("/queryBook")
    public ModelAndView queryBook(Book book){
        System.out.println(book.getId());
        List<Book> list = mainService.queryBook(book);
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("bookList",list);
        return mv;
    }

//    推荐
    @RequestMapping("/recommend")
    public ModelAndView recommend(){
        List<Book> list = mainService.recommend();
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("bookList",list);
        return mv;
    }

//    猜你喜欢
    @RequestMapping("/enjoy")
    public ModelAndView enjoy(){
        List<Book> list = mainService.enjoy();
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("bookList",list);
        return mv;
    }

//    查询单个书籍详细信息
    @RequestMapping("/queryOneBook")
    public ModelAndView queryOneBook(int id){
//        得到当前书籍
        Book thisBook = mainService.queryOneBook(id);
//        得到书籍的评论
        List<Mess> messList = mainService.queryBookMess(id);
//        得到同类产品推荐
        List<Book> list = mainService.recommend();
//        点击量加一乘以1.3
        mainService.addClick(id);

        Gson gson = new Gson();
        String sss = gson.toJson(messList);

        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("thisBook",thisBook);//当前书籍
        mv.addObject("messList",sss);//书评
        mv.addObject("bookList",list);//同类书籍推荐
        return mv;
    }

//    添加购物车
    @RequestMapping("/addBookToShop")
    public ModelAndView addBookToShop(Book book, int customID,int number){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        //先判断购物车中是否存在该用户的该书籍，如果存在，则进行加操作
        int count = mainService.queryShop(customID,book.getId());
        if(count==0){//不存在
            mv.addObject("flag",mainService.addBookToShop(book,customID,number));
        }else{//存在
            mv.addObject("flag",mainService.updateBookNumToShop(book,customID,number));
        }
        return mv;
    }

//    判断图书是否参与了活动
    @RequestMapping("/joinActive")
    public ModelAndView joinActive(int id){
        ModelAndView mv = new ModelAndView();
        //        得到当前书籍的Btype编号
        int btypeId = mainService.queryBtypeByBookId(id);
        List<Active> activeList = mainService.queryAllActive();
        for(Active active : activeList){//遍历所有活动
            if (btypeId==active.getBtype().getId()){//如果当前图书参与了活动，则将活动传到前台
                mv.addObject("active",active);
                break;
            }
        }
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }


//    跳转视图
    @RequestMapping("/skipToTalk")
    public ModelAndView skipToTalk(){
        return new ModelAndView("stage/LQ_customTalk");
    }

    //    跳转视图
    @RequestMapping("/LQ_talk")
    public ModelAndView LQ_talk(){
        return new ModelAndView("backstage/LQ_talk");
    }

    //    跳转视图
    @RequestMapping("/LQ_details")
    public ModelAndView LQ_details(){
        return new ModelAndView("stage/LQ_details");
    }

    public void setMainService(LQMainService mainService) {
        this.mainService = mainService;
    }
}
